package com.yueqian.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yueqian.system.converter.RoleMenuConvert;
import com.yueqian.system.entity.RoleMenu;
import com.yueqian.system.mapper.RoleMenuMapper;
import com.yueqian.system.service.RoleMenuService;
import com.yueqian.system.vo.req.RoleMenuReqVO;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
	@Override
	public Page<RoleMenu> selectPageData(RoleMenuReqVO reqVO) {
		IPage<RoleMenu> page = reqVO.getPage(reqVO.getCurrentPage(), reqVO.getPageSize());
		return (Page<RoleMenu>) this.baseMapper.selectPage(page, null);
	}

	@Override
	public List<RoleMenu> selectListData(RoleMenuReqVO reqVO) {
		return this.list();
	}

	@Transactional
	@Override
	public Boolean add(List<RoleMenuReqVO> reqVO) {
		if (CollectionUtil.isEmpty(reqVO)) {
			return false;
		}
		List<RoleMenu> list = RoleMenuConvert.INSTANCE.toList(reqVO);
		return this.saveBatch(list);
	}

	@Override
	public Long add(RoleMenuReqVO reqVO) {
		RoleMenu entity = RoleMenuConvert.INSTANCE.toEntity(reqVO);
		//编辑
		if (ObjectUtil.isNotNull(entity.getId())) {
			this.saveOrUpdate(entity);
		}
		//新增
		this.saveOrUpdate(entity);
		return entity.getId();
	}

	@Transactional
	@Override
	public Boolean delete(List<Long> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return false;
		}
		return this.removeByIds(ids);
	}

	@Override
	public Long delete(Long id) {
		this.removeById(id);
		return id;
	}

	@Override
	public List<RoleMenu> getDetail(List<Long> ids) {
		if (CollectionUtil.isNotEmpty(ids)) {
			ArrayList<RoleMenu> roleMenuList = new ArrayList<>();
			roleMenuList.add(new RoleMenu());
			return roleMenuList;
		}
		return this.listByIds(ids);
	}

	@Override
	public RoleMenu getDetail(Long id) {
		//新增
		if (ObjectUtil.isEmpty(id)) {
			return new RoleMenu();
		} else {//编辑
			return this.getById(id);
		}
	}


}
